package com.socket.server;

import com.socket.exception.TimeoutException;

/**
 * 服务器响应
 * @author luoweiyi
 *
 */
public class ResponsePer {
	
	private boolean answer = false;
	
	private char status;
	
	private String msg;
	
	private Object value;
	
	private static Long TIME_OUT = 1000L;
	
	public void setValue(Object value){
		this.value = value;
	}
	
	/**
	 * @return
	 * @throws InterruptedException
	 */
	public Object getValue() throws InterruptedException{
		return value;
	}

	/**
	 * 获取调用接口状态
	 * @return
	 * @throws InterruptedException
	 */
	public synchronized char getStatus() throws InterruptedException {
		Long start = System.currentTimeMillis();
		System.out.println("开始计时");
		while(!answer){
			Long now = System.currentTimeMillis();
			Long rest = now - start;
			if(rest > TIME_OUT)
				throw new TimeoutException("Thread "+Thread.currentThread().getName() +" request timeout!!!");
			wait(TIME_OUT);
		}
		return status;
	}

	public synchronized void setStatus(char status) {
		this.status = status;
		answer = true;
		notifyAll();
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
}
